package com.hamizhong.iflowers.common.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.hamizhong.iflowers.common.cache.MyCacheFactory;
import com.hamizhong.iflowers.common.cache.MyCacheParam;
import com.hamizhong.iflowers.common.logger.MyLogger;
import com.hamizhong.iflowers.common.logger.MyLoggerFactory;

public class IFlowersServletContextListener implements ServletContextListener {

	MyLogger logger = MyLoggerFactory.getLogger(this.getClass());

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		logger.info("***************************************************");
		logger.info("* Server starting, begin to initialize data.      *");
		logger.info("***************************************************");
		ServletContext servletContext = servletContextEvent.getServletContext();
		
		//初始化缓存
		logger.info("========== Start to load IFlowers cache. ============");
		
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_VARIETY_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_COLOR_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_PURPOSE_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_TARGET_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_FESTIVAL_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_FLOWERFLORID_DB_ATTR);
		MyCacheFactory.getInstance().refreshCache(servletContext, MyCacheParam.C_IFLOWERS_FLOWERCOUNT_DB_ATTR);

		logger.info("========== Load IFlowers cache Successfull.==========");
		
		logger.info("***************************************************");
		logger.info("* Server starting, initialize data - Successfull. *");
		logger.info("***************************************************");
	}
	
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		try {
			logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
			logger.info("Server destroyed.");
			logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		} catch (Exception e) {
		}
	}

}
